package org.jeecg.modules.simulator.model.train.manager;

import org.jeecg.modules.simulator.model.line.plan.TrainPlanTablePath;
import lombok.Getter;
import lombok.Setter;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: gp
 * @description:
 * @author: lydms
 * @create: 2024-08-08 10:00
 **/
public class TrainCalculateBizManager {
    @Getter
    @Setter
    private Map<String, List<String>> lineMap;
    @Getter
    @Setter
    private Map<String, TrainPlanTablePath> trainPlanTablePathMap;

    @Getter
    @Setter
    private String lineId;


    /**
     * @description: 发车时间表，key：发车时间，value：发车时间对应：列车id——列车车次号
     *
     * @author:  renzhg
     * @date:  2024/9/2 14:06
     **/
    @Getter
    @Setter
    private    Map<String,List<TrainPlanTablePath>> startTrainTimeMap=new LinkedHashMap<>();
//     /**
//     * @description: 获取线路对应的车辆信息
//     *
//     *
//     * @author: renzhg
//     * @date: 2024/8/6 11:07
//     **/
//    public Map<String, TrainPlanTablePath> getTrainPlanTablePathMapByline(String line){
//        List<String> trainnumbs = lineMap.get(line);
//        Map<String, TrainPlanTablePath> result = new LinkedHashMap<>();
////从这里得出nearestTimePoint值最小的trainId
//
//        for (String trainId : trainnumbs) {
//            TrainPlanTablePath trainPlanTables = trainPlanTablePathMap.get(trainId);
//            result.put(trainId, trainPlanTables);
//
//        }
//
//
//        return result;
//    }
    /**
     * @description: 给定时间点和站点，到是否有车在时间点上挺在这个站上，如果有返回map不为空，否则返回空map
     *
     * @author: renzhg
     * @date: 2024/8/6 14:22
     **/

    Map<String, TrainPlanTablePath>  getStationPiontTimeHavedTrain(String PT,String ST,Map<String, TrainPlanTablePath> trainPlanTablePathMap){
        Map<String, TrainPlanTablePath> result = new LinkedHashMap<>();
        for (String key : trainPlanTablePathMap.keySet()) {
            TrainPlanTablePath trainPlanTablePath = trainPlanTablePathMap.get(key);
            if(trainPlanTablePath.isTimePointInStationTime(PT,ST)){
                result.put(key, trainPlanTablePath);
            }
        }
        return result;
    }

    /**
     * @description: 给定的时间点和站点，找出最近到达时间点的车辆
     *
     * @author: renzhg
     * @date: 2024/8/6 15:35
     **/
    //nearestArriveTimePoint找出最小的值
    Map<String, TrainPlanTablePath>   getStationPiontTimeNoTrain(String PT,String ST,Map<String, TrainPlanTablePath> trainPlanTablePathMap){
        int min = Integer.MAX_VALUE;
        Map<String, TrainPlanTablePath> result = new LinkedHashMap<>();
        for (String key : trainPlanTablePathMap.keySet()) {
            TrainPlanTablePath trainPlanTablePath = trainPlanTablePathMap.get(key);
            int nearestArriveTimePoint = trainPlanTablePath.getNearestArriveTimePoint(PT, ST);
            if (nearestArriveTimePoint < min) {
                min = nearestArriveTimePoint;
                result.clear();
                result.put(key, trainPlanTablePath);
            } else if (nearestArriveTimePoint == min) {
                result.put(key, trainPlanTablePath);
            }
        }
        return result;





    }

    /**
     * @description: 根据TrainPlanTablePath.getFirstTime()，排序TrainPlanTablePath
     *
     * @author: renzhg
     * @date: 2024/8/6 15:49
     **/
    public Map<String, TrainPlanTablePath> sortTrainByFirstTime(Map<String, TrainPlanTablePath> trainPlanTablePathMap) {
        return trainPlanTablePathMap.entrySet()
                .stream()
                .sorted(Comparator.comparing(entry -> entry.getValue().getFirstTime()))
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (e1, e2) -> e1, // 处理键冲突的合并函数
                        LinkedHashMap::new // 保持插入顺序
                ));
    }

    /**
     * @description: 找到制定列车时刻表周围几个时刻表
     *
     * @author: renzhg
     * @date: 2024/8/6 16:09
     **/
    public List<TrainPlanTablePath> findAdjacentTrains(List<TrainPlanTablePath> trainList, String key,int num) {
        List<TrainPlanTablePath> result = new ArrayList<>();

        for (int i = 0; i < trainList.size(); i++) {
            TrainPlanTablePath currentTrain = trainList.get(i);
            if (currentTrain.getTrainId().equals(key)) {
                // 添加当前列车
                result.add(currentTrain);

                // 添加前一个列车
                if (i > 0) {
                    TrainPlanTablePath previousTrain = trainList.get(i - 1);
                    result.add(previousTrain);
                }

                // 添加后一个列车
                for (int m = 0; m < num - 2; m++) {
                    if (i < trainList.size() - 1) {
                        TrainPlanTablePath nextTrain = trainList.get(i + m);
                        result.add(nextTrain);
                    }
                }


            }


        }
        return result; // 返回结果
    }



//根据TrainPlanTablePath.getFirstTime()，排序TrainPlanTablePath

}
